﻿/* QF classbuilder for Enlib */
using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Logging;

#region SYS_RolePrivilege实体类
public class SYS_RolePrivilege
{
	private string m_RoleID;
	private string m_PrivilegeID;
	
	public SYS_RolePrivilege ()
	{
		m_RoleID="";
		m_PrivilegeID="";
	}
	
	public string RoleID
	{
		get{return m_RoleID;}
		set{m_RoleID=value;}
	}
	public string PrivilegeID
	{
		get{return m_PrivilegeID;}
		set{m_PrivilegeID=value;}
	}
}
#endregion

#region SYS_RolePrivilege管理类
public partial class LogicLayer
{
	public bool AddSYS_RolePrivilege(SYS_RolePrivilege theSYS_RolePrivilege)
	{
		try
		{
			Database db = DatabaseFactory.CreateDatabase("connStr");
			string sql = "insert into SYS_RolePrivilege (RoleID,PrivilegeID) values (@RoleID,@PrivilegeID)";
			System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(sql);
			
			db.AddInParameter(cmd, "@RoleID", DbType.String, theSYS_RolePrivilege.RoleID);
			db.AddInParameter(cmd, "@PrivilegeID", DbType.String, theSYS_RolePrivilege.PrivilegeID);
			
			db.ExecuteNonQuery(cmd);
			return true;
		}
		catch(Exception ex)
		{
			Logger.Write("AddSYS_RolePrivilege:" + ex.Message );
			return false;
		}
	}
	
	public SYS_RolePrivilege GetSYS_RolePrivilegeFromDataReader(IDataReader dataReader)
	{
		try
		{
			SYS_RolePrivilege result = new SYS_RolePrivilege();
			
			if (dataReader["RoleID"] != null && dataReader["RoleID"] != System.DBNull.Value )
				result.RoleID = (string)dataReader["RoleID"];
			if (dataReader["PrivilegeID"] != null && dataReader["PrivilegeID"] != System.DBNull.Value )
				result.PrivilegeID = (string)dataReader["PrivilegeID"];
			
			return result;
		}
		catch(Exception ex)
		{
			Logger.Write("GetReaderSYS_RolePrivilege:" + ex.Message);
			return null;
		}
	}
	
	public SYS_RolePrivilege GetSYS_RolePrivilege(string RoleID,string PrivilegeID)
	{
		try
		{
			Database db = DatabaseFactory.CreateDatabase("connStr");
			string sql = "select * From SYS_RolePrivilege where RoleID=@RoleID and PrivilegeID=@PrivilegeID";
			
			System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(sql);
			db.AddInParameter(cmd, "@RoleID", DbType.String, RoleID);
			db.AddInParameter(cmd, "@PrivilegeID", DbType.String, PrivilegeID);
			
			SYS_RolePrivilege result = null;
			using (IDataReader dataReader = db.ExecuteReader(cmd))
			{
				if (dataReader.Read())
				{
					result = GetSYS_RolePrivilegeFromDataReader( dataReader );
				}
			}
			return result;
		}
		catch(Exception ex)
		{
			Logger.Write("GetSYS_RolePrivilege:" + ex.Message);
			return null;
		}
	}
	
	public bool DelSYS_RolePrivilege(string RoleID,string PrivilegeID)
	{
		try
		{
			Database db = DatabaseFactory.CreateDatabase("connStr");
			string sql = "delete From SYS_RolePrivilege where RoleID=@RoleID and PrivilegeID=@PrivilegeID";
			
			System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(sql);
			db.AddInParameter(cmd, "@RoleID", DbType.String, RoleID);
			db.AddInParameter(cmd, "@PrivilegeID", DbType.String, PrivilegeID);
			
			db.ExecuteNonQuery(cmd);
			return true;
		}
		catch(Exception ex)
		{
			Logger.Write("DelSYS_RolePrivilege:" + ex.Message);
			return false;
		}
	}
	
	public bool UpdateSYS_RolePrivilege(SYS_RolePrivilege theSYS_RolePrivilege)
	{
		try
		{
			Database db = DatabaseFactory.CreateDatabase("connStr");
			string sql = "update SYS_RolePrivilege set  where RoleID=@RoleID and PrivilegeID=@PrivilegeID";
			System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(sql);
			
			db.AddInParameter(cmd, "@RoleID", DbType.String, theSYS_RolePrivilege.RoleID);
			db.AddInParameter(cmd, "@PrivilegeID", DbType.String, theSYS_RolePrivilege.PrivilegeID);
			
			db.ExecuteNonQuery(cmd);
			return true;
		}
		catch(Exception ex)
		{
			Logger.Write("UpdateSYS_RolePrivilege:" + ex.Message);
			return false;
		}
	}
	
	public List<SYS_RolePrivilege> ListSYS_RolePrivilege()
	{
		try
		{
			Database db = DatabaseFactory.CreateDatabase("connStr");
			string sql = "select * from SYS_RolePrivilege";
			System.Data.Common.DbCommand cmd = db.GetSqlStringCommand(sql);
			
			 List<SYS_RolePrivilege> result = new  List<SYS_RolePrivilege>();
			using (IDataReader dataReader = db.ExecuteReader(cmd))
			{
				while (dataReader.Read())
				{
					SYS_RolePrivilege obj = GetSYS_RolePrivilegeFromDataReader( dataReader );
					if(obj != null)
						result.Add(obj);
				}
			}
			return result;
		}
		catch(Exception ex)
		{
			Logger.Write("ListSYS_RolePrivilege:" + ex.Message);
			return null;
		}
	}
	
}
#endregion